﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Jinjia.Approve.Entity;
using Jinjia.Approve.Entity.Entity;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM;
using Jinjia.Core.BaseInfrastructure.StorageModel.Model;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using SqlSugar;

namespace Jinjia.Approve.Repository.JJFlow
{
    public class FlowDicCodeRepository : IFlowDicCodeRepository
    {
        private readonly ISqlSugarClient _db;

        public FlowDicCodeRepository(IUnitOfWork unitOfWork)
        {
            _db = unitOfWork.GetDbClient();
        }

        public async Task<PageListSearchResultVM<JJDictCodeViewModel>> GetMappingPageList(JJDictCodeSearchCondition searchCondition, int user_id)
        {
            var queryable = _db.Queryable<jj_flow_dictcode, erpa_user_tbl, erpa_user_tbl, erpa_base_tbl>((a, d, e, f) =>
                new JoinQueryInfos(
                    JoinType.Left, a.create_user_id == d.id,
                    JoinType.Left, a.update_user_id == e.id,
                    JoinType.Left, f.code_type == "common_bool" && SqlFunc.ToInt32(a.is_push_feishu).ToString() == f.value
                ));

            queryable.Where(a => a.deleted == 0);

            queryable.Where(a => a.template_id == searchCondition.template_id);

            if (!string.IsNullOrWhiteSpace(searchCondition.dictcode_name))
            {
                queryable.Where(a => a.dictcode_name.Contains(searchCondition.dictcode_name));
            }

            if (!string.IsNullOrWhiteSpace(searchCondition.dictcode_key))
            {
                queryable.Where(a => a.dictcode_key.Contains(searchCondition.dictcode_key));
            }

            var orderby = " a.create_time desc";
            queryable.OrderBy(!string.IsNullOrWhiteSpace(searchCondition.order_by) ? searchCondition.order_by + " " + searchCondition.direction : orderby);


            var newQuery = queryable.Select<JJDictCodeViewModel>(@" a.id,a.dictcode_name,a.dictcode_key, a.is_push_feishu, f.name as is_push_feishu_text, a.sort_number, a.remark,a.create_time,a.update_time,a.remark,
             d.user_name as create_user_name,e.user_name as update_user_name ");

            var result = await newQuery.ToPageListAsync(searchCondition, new DynamicTableHeaderInput()
            {
                MethodName = "ERPAssistant.Api.Controllers.JJFlowController.GetDictCodePageList",
                UserId = user_id
            });

            return result;
        }

        public async Task<jj_flow_dictcode> GetById(int id)
        {
            var dictcode = await _db.Queryable<jj_flow_dictcode>().Where(x => x.id == id).FirstAsync();
            return dictcode;
        }

        public async Task<List<jj_flow_dictcode>> GetListByTemplateId(int template_id)
        {
            var list = await _db.Queryable<jj_flow_dictcode>().Where(x => x.template_id == template_id && x.deleted == 0).ToListAsync();
            return list;
        }

        public async Task<int> GetDicCodeCount(string dictcode_key, int id, int template_id)
        {
            var dictCodeCount = await _db.Queryable<jj_flow_dictcode>()
                .Where(t => t.dictcode_key == dictcode_key)
                .Where(t => t.id != id)
                .Where(t => t.deleted == 0)
                .Where(t => t.template_id == template_id)
                .CountAsync();
            return dictCodeCount;
        }

        public async Task<int> Save(jj_flow_dictcode data)
        {
            if (data.id <= 0) //新增
            {
                var id = await _db.Insertable(data).ExecuteReturnIdentityAsync();
                return id;
            }
            else
            {
                await _db.Updateable(data)
                    .Where(x => x.id == data.id)
                    .ExecuteCommandAsync();
                return data.id;
            }
        }

        public async Task LogicalDelete(int id, int user_id)
        {
            await _db.Updateable<jj_flow_dictcode>()
                .SetColumns(x => new jj_flow_dictcode { update_user_id = user_id, update_time = DateTime.Now, deleted = 1 })
                .Where(x => x.id == id)
                .ExecuteCommandAsync();
        }
    }
}